#include "productdatabaseoperations.h"
#include <QDebug>
#include "common.h"
#include "tabledefine.h"

QSqlDatabase productDB;

bool connectProductDB(QString dbName)
{
    if (QSqlDatabase::contains(PRODUCT_CONNECTION))
    {
        productDB = QSqlDatabase::database(PRODUCT_CONNECTION);
    }
    else
    {
        productDB = QSqlDatabase::addDatabase("QSQLITE", PRODUCT_CONNECTION);
        productDB.setDatabaseName(dbName);
        productDB.setUserName("yanshang");
        productDB.setPassword("912");
    }
    if(!productDB.open())
    {
        qCritical()<<"file:"<<__FILE__<<"line:"<<__LINE__<<productDB.lastError();
        return false;
    }
    qDebug()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"连接并打开设备数据库成功";
    return true;
}

QString selectProductName()
{
    QSqlQuery query(productDB);
    QString sql = QString("select %1 from %2").arg(NAME, PRODUCT_INFO_TABLE_NAME);
    if(!query.exec(sql))
    {
        qCritical()<<"file:"<<__FILE__<<"line:"<<__LINE__<<query.lastError();
        return NULL;
    }
    if(query.first())
    {
        qDebug()<<"file:"<<__FILE__<<"line:"<<__LINE__<<"获取产品名成功";
        return query.value(0).toString();
    }
    return NULL;
}
